/**
 * [spring-jpa-distributed-transaction] in.choich.services.JustTestTranService.java
 * 
 * created at 2016年11月1日 by kevin_zhu
 */
package in.choich.services;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import in.choich.entities.jpa.CourseModel;
import in.choich.entities.jpa.StudentModel;
import in.choich.entities.jpa2.CityModel;
import in.choich.repositories.jpa.CourseRepository;
import in.choich.repositories.jpa.StudentRepository;
import in.choich.repositories.jpa2.CityModelRepository;

/**
 * @author kevin_zhu
 *
 */
@Service
@Transactional
public class JustTestTranService {

private static final Logger log = LoggerFactory.getLogger(JustTestTranService.class);
	
//	@Autowired
//	private PlatformTransactionManager transactionManager;

	@Autowired
	private StudentRepository studentRepository;
	
	@Autowired
	private CityModelRepository cityRepository;
	
	@Autowired
	private CourseRepository courseRepository;
	
	public JustTestTranService(){
		
	}
	
	public void anoth(){
//		CityModel c = new CityModel();
//		c.setCountryCode("CHN");
//		c.setPopulation(20000);
//		c.setName("beijing");
//		c.setDistrict("chaoyang");
//		
//		CityModel city = cityRepository.save(c);
//		log.info(""+city);
//		log.info("Citymode Saved successfully.");
		CourseModel course = new CourseModel();
		course.setCno("C007");
		course.setScoure(3);
		course.setClassHour(6);
		log.info("成绩："+course.getScoure());
		courseRepository.save(course);
	}
	
	public boolean multiDataSourceTransaction(){
		log.info("now Excuting multiDataSourceTransaction....");
		boolean isCity = true;
		boolean isStu = true;
//		try {
			log.info("now Excuting multiDataSourceTransaction....CityModel");
			anoth();
//		} catch (Exception e) {
//			// TODO: handle exception
//			log.error("Citymode 执行出错了："+e.getMessage());
			isCity = false;
//		}
//		
//		try {
			
			
			log.info("now Excuting multiDataSourceTransaction....StudentModel");
			StudentModel stu = new StudentModel();
			stu.setFirstName("killy");
			stu.setLastName("teq");
			stu.setAge(17);
			// 不给male字段赋值，导致更新失败，因有@Transactional保证事务一致性，stuRela的更新操作也会失败
			// 如果没有@Transactional，stu的失败不会影响stuRela
			stu.setGender("male");
			stu.setSno("S0022");
			studentRepository.save(stu);
			
			StudentModel stu1 = new StudentModel();
			stu1.setFirstName("killy");
			stu1.setLastName("teq");
			stu1.setAge(17);
			stu1.setGender("female");
//			stu1.setSno("S0023");
			studentRepository.save(stu1);
			log.info("StudentModel Saved successfully.");
			
			
//		} catch (Exception e) {
//			// TODO: handle exception
//			log.error("StudentModel 执行出错了："+e.getMessage());
			isStu = false;
//		}
		log.info("Already Excuted multiDataSourceTransaction....");
		
		return isCity && isStu;
		
	}
	
}
